package org.nanotek.lucene.job.writers;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericField;
import org.apache.lucene.index.IndexWriter;
import org.nanotek.beans.TrackName;
import org.springframework.batch.item.ItemWriter;

/**
 * @deprecated
 * @author josecanova
 *
 */
public class TrackNameIndexWriter implements ItemWriter<TrackName> {

	private IndexWriter indexWriter;
	private Log log = LogFactory.getLog(TrackNameIndexWriter.class);
	
	@Override
	public void write(List<? extends TrackName> items) throws Exception {
		log.info("WRITE Method TrackNameIndexWriter");
		for (TrackName releaseName : items) 
			indexWriter.addDocument(createTemplate(releaseName));
	}

	public IndexWriter getIndexWriter() {
		return indexWriter;
	}

	public void setIndexWriter(IndexWriter indexWriter) {
		this.indexWriter = indexWriter;
	}

	public  Document createTemplate(TrackName releaseName) throws Exception {

		Document doc = new Document();


		Field wordField = new Field("id", releaseName.getId(), Field.Store.YES, Field.Index.ANALYZED); //$NON-NLS-1$ //$NON-NLS-2$
		wordField.setOmitTermFreqAndPositions(true);
		wordField.setOmitNorms(true);
		doc.add(wordField);

		Field anmeField = new Field("name", releaseName.getPage(), Field.Store.YES, Field.Index.ANALYZED); //$NON-NLS-1$ //$NON-NLS-2$
		wordField.setOmitTermFreqAndPositions(true);
		wordField.setOmitNorms(true);
		doc.add(anmeField);

		NumericField modifiedField = new NumericField("modified"); //$NON-NLS-1$
		modifiedField.setLongValue(new Date().getTime());
		doc.add(modifiedField);

		return doc;
	}
}
